package org.ljy.rabbitmqspringboot.controller;

import org.ljy.rabbitmqspringboot.contant.Constants;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/produce")
@RestController
public class ProducerController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/work")
    public String work() {
        for (int i = 0; i < 10; i++) {
            String msg = "这是工作队列的一条消息:"+i;
            rabbitTemplate.convertAndSend("", Constants.WORK_QUEUE,msg.getBytes());
        }
        return "消息发送成功";
    }

    @RequestMapping("/fanout")
    public String fanout() {
        rabbitTemplate.convertAndSend(Constants.FANOUT_EXCHANGE,"","我是一条fanout消息");
        return "fanout消息发送成功";
    }

    @RequestMapping("/direct/{routingKey}")
    public String direct(@PathVariable("routingKey") String routingKey) {
        rabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE,routingKey,"我是一条direct消息，routingKey为:"+routingKey);
        return "direct消息发送成功";
    }

    @RequestMapping("/topic/{routingKey}")
    public String topic(@PathVariable("routingKey") String routingKey) {
        rabbitTemplate.convertAndSend(Constants.TOPIC_EXCHANGE,routingKey,"我是一条topic消息，routingKey为："+routingKey);
        return "topic消息发送成功";
    }


}
